package io.sqrtqiezi.spark.dataframe

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._

object TypeOperator {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder()
      .master("local")
      .appName("flight data")
      .getOrCreate()

    val df = spark.read.format("csv")
      .option("inferSchema", "true")
      .option("header", "true")
      .load("data/retail-data/by-day/2010-12-01.csv")

    df.printSchema

    val fabricateQuantity = pow(col("Quantity") * col("UnitPrice"), 2) + 5
    println(fabricateQuantity.toString)
    df.select(expr("CustomerID"), fabricateQuantity.alias("realQuantity")).show(10)

    println(df.stat.corr("Quantity", "UnitPrice"))
    df.select(corr("Quantity", "UnitPrice")).show

    println(current_date())
    println(current_timestamp())

    val dateDF = spark.range(10)
      .withColumn("today", current_date())
      .withColumn("now", current_timestamp())

    dateDF.printSchema
    dateDF.show(truncate = false)
  }
}
